{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-01-07T07:39:10.028120Z",
     "start_time": "2025-01-07T07:39:09.364705Z"
    }
   },
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ],
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T07:39:32.360035Z",
     "start_time": "2025-01-07T07:39:32.336778Z"
    }
   },
   "cell_type": "code",
   "source": [
    "ser_obj = pd.Series(np.random.randn(12),index=[['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c', 'd', 'd',\n",
    "'d'],[0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2]])\n",
    "print(ser_obj)"
   ],
   "id": "75bdfe06a425ab2c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a  0   -0.571435\n",
      "   1   -0.385194\n",
      "   2   -0.441781\n",
      "b  0    0.986818\n",
      "   1   -0.723956\n",
      "   2    1.415911\n",
      "c  0    0.679603\n",
      "   1    0.295595\n",
      "   2   -2.238612\n",
      "d  0    0.751870\n",
      "   1   -0.153732\n",
      "   2   -0.721032\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T07:40:05.073583Z",
     "start_time": "2025-01-07T07:40:05.064146Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(type(ser_obj.index))\n",
    "print(ser_obj.index)"
   ],
   "id": "eea7772906b15358",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.indexes.multi.MultiIndex'>\n",
      "MultiIndex([('a', 0),\n",
      "            ('a', 1),\n",
      "            ('a', 2),\n",
      "            ('b', 0),\n",
      "            ('b', 1),\n",
      "            ('b', 2),\n",
      "            ('c', 0),\n",
      "            ('c', 1),\n",
      "            ('c', 2),\n",
      "            ('d', 0),\n",
      "            ('d', 1),\n",
      "            ('d', 2)],\n",
      "           )\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T07:40:32.062974Z",
     "start_time": "2025-01-07T07:40:32.056300Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 外层选取\n",
    "print(ser_obj['c'])"
   ],
   "id": "f4e28a4ad51308a0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    0.679603\n",
      "1    0.295595\n",
      "2   -2.238612\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T07:40:50.365135Z",
     "start_time": "2025-01-07T07:40:50.356225Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 内层选取\n",
    "print(ser_obj[:, 2])"
   ],
   "id": "4c112c1af19cfc12",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a   -0.441781\n",
      "b    1.415911\n",
      "c   -2.238612\n",
      "d   -0.721032\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T07:41:20.248031Z",
     "start_time": "2025-01-07T07:41:20.241592Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#交换内层与外层索引\n",
    "print(ser_obj.swaplevel())"
   ],
   "id": "6c8eaa8cb56d72e2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0  a   -0.571435\n",
      "1  a   -0.385194\n",
      "2  a   -0.441781\n",
      "0  b    0.986818\n",
      "1  b   -0.723956\n",
      "2  b    1.415911\n",
      "0  c    0.679603\n",
      "1  c    0.295595\n",
      "2  c   -2.238612\n",
      "0  d    0.751870\n",
      "1  d   -0.153732\n",
      "2  d   -0.721032\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T07:41:36.847929Z",
     "start_time": "2025-01-07T07:41:36.829090Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#将具有 MultiIndex 的 Unstack（也称为数据透视图） 系列生成 DataFrame。 涉及的级别将自动进行排序\n",
    "print(ser_obj.unstack(0))"
   ],
   "id": "8fe7d1dd0ef69cd2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          a         b         c         d\n",
      "0 -0.571435  0.986818  0.679603  0.751870\n",
      "1 -0.385194 -0.723956  0.295595 -0.153732\n",
      "2 -0.441781  1.415911 -2.238612 -0.721032\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T07:42:16.049237Z",
     "start_time": "2025-01-07T07:42:16.036170Z"
    }
   },
   "cell_type": "code",
   "source": [
    "ser_obj2 = pd.Series(np.random.randn(12), index = [\n",
    "['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c', 'd', 'd', 'd'],\n",
    "[0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2]\n",
    "])\n",
    "print(ser_obj2)"
   ],
   "id": "55fca09ccd74bbab",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a  0   -1.333215\n",
      "   1   -0.268614\n",
      "   2    0.684657\n",
      "b  0   -0.581435\n",
      "   1    0.940238\n",
      "   2   -0.093836\n",
      "c  0   -1.098725\n",
      "   1    0.537848\n",
      "   2    0.884247\n",
      "d  0   -0.872861\n",
      "   1    0.587313\n",
      "   2   -0.503094\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 8
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
